package com.hgk.common.common.ui.view;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Gallery;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by lenovo on 2016/10/14.
 * 轮播类
 */
public class BannerGuidGallery extends Gallery {

    private Thread timeThread = null;
    private boolean timeFlag = true;
    private boolean isExit = false;
    private ImageTimerTask timerTask = null;
    private int gallerypisition = 0;
    private Timer autoGallery;

    public BannerGuidGallery(Context context) {
        super(context);
    }
    public BannerGuidGallery(Context context,AttributeSet attrs){
        super(context,attrs);
    }
    public  BannerGuidGallery(Context context,AttributeSet attrs,int defStyle){
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        int kEvent;
        if (isScrollingLeft(e1,e2)){
            kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
        }else{
            kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
        }
        onKeyDown(kEvent,null);
        if (this.getSelectedItemPosition()==0)
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                  timeFlag = false;
                    this.cancel();
                }
            },3000);
        return true;
    }

    private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {

        return e2.getX() > e1.getX();
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return super.onScroll(e1, e2, distanceX, distanceY);
    }

    public void startScrollIma(){
        autoGallery = new Timer();
        timerTask = new ImageTimerTask();
        autoGallery.scheduleAtFixedRate(timerTask,
                5000,//第一次轮播时间
                5000);//以后每一次轮播时间
        timeThread = new Thread(){
            @Override
            public void run() {
                super.run();
                while (!isExit){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (timerTask){
                        if (!timeFlag){
                            timerTask.timeCondition=true;
                            timerTask.notifyAll();
                        }
                    }
                    timeFlag = true;
                }
            };
        };
        timeThread.start();
    }

    public void stopScrolImg(){
        autoGallery.cancel();
    }

    //轮播时 时间的设定：异步执行延时操作
    public class ImageTimerTask extends TimerTask{
        //volatile [化学] 挥发性的；不稳定的；爆炸性的；反复无常的
        public volatile boolean timeCondition = true;
        @Override
        public void run() {
            synchronized (this){
                while(!timeCondition){
                    try{
                        Thread.sleep(1000);
                        wait();
                    }catch(InterruptedException e){
                        Thread.interrupted();
                    }
                }
            }
            try{
                gallerypisition = getSelectedItemPosition()+1;
                Message msg = new Message();
                Bundle bundle = new Bundle();
                bundle.putInt("pos",gallerypisition);
                msg.setData(bundle);
                msg.what = 1;
                autoGalleryHandler.sendMessage(msg);
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
    final Handler autoGalleryHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    try{
                        onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT,null);
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                    break;
            }
        }
    };
}
